/**
 * fulihui.com Inc.
 * Copyright (c) 2015-2016 All Rights Reserved.
 */
package com.fulihui.integral.core.service;

import com.alibaba.fastjson.JSON;
import com.fulihui.integral.core.api.ISBaseRuleService;
import com.fulihui.integral.core.biz.conv.ISRuleConvert;
import com.fulihui.integral.core.common.entity.RuleData;
import com.fulihui.integral.core.dal.dataobj.ISRuleInfo;
import com.fulihui.integral.core.dal.dataobj.ISRuleInfoExample;
import com.fulihui.integral.core.dal.mapper.ISRuleInfoMapper;
import com.fulihui.integral.core.dto.ISBaseRuleDTO;
import com.fulihui.integral.core.request.ISBaseRuleQueryReq;
import com.fulihui.integral.core.request.ISBaseRuleReq;
import com.fulihui.integral.core.result.ISBaseRuleResult;
import com.fulihui.integral.core.result.ISRulePageResult;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.toolkit.common.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 规则查询服务
 *
 * @author yunfeng.li
 * @version $Id: v 0.1 2017年04月17日 15:20 yunfeng.li Exp $
 */
@Service("iSBaseRuleService")
public class ISBaseRuleServiceImpl implements ISBaseRuleService {

    @Resource
    private ISRuleInfoMapper iSRuleInfoMapper;

    /**
     * 根据id查询单个规则配置
     *
     * @param req
     * @return
     */
    @Override
    public ISBaseRuleResult queryISRuleById(ISBaseRuleQueryReq req) {
        ISBaseRuleResult result = ResultBuilder.succ(ISBaseRuleResult.class);
        if (req == null || req.getRuleId() == null) {
            result.setErrmsg("ruleID不能为空");
            return result;
        }
        ISRuleInfo isRuleInfo = iSRuleInfoMapper.selectByPrimaryKey(req.getRuleId());
        ISBaseRuleDTO isBaseRuleDTO = ISRuleConvert.convertDO2DTO(isRuleInfo);
        result.setIsBaseRuleDTO(isBaseRuleDTO);
        return result;
    }

    /**
     * 根据渠道查询单个规则配置
     *
     * @param req
     * @return
     */
    @Override
    public ISBaseRuleResult queryISRuleByChannel(ISBaseRuleQueryReq req) {
        ISBaseRuleResult result = ResultBuilder.succ(ISBaseRuleResult.class);
        if (req == null || req.getChannel() == null) {
            result.setErrmsg("ruleID不能为空");
            return result;
        }
        ISRuleInfoExample example = new ISRuleInfoExample();
        ISRuleInfoExample.Criteria criteria = example.createCriteria();
        criteria.andChannelEqualTo(req.getChannel());
        List<ISRuleInfo> isRuleInfos = iSRuleInfoMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(isRuleInfos)) {
            ISBaseRuleDTO isBaseRuleDTO = ISRuleConvert.convertDO2DTO(isRuleInfos.get(0));
            result.setIsBaseRuleDTO(isBaseRuleDTO);
        }
        return result;
    }

    /**
     * 查询列表
     *
     * @param req
     * @return
     */
    @Override
    public ISRulePageResult queryISRulePage(ISBaseRuleQueryReq req) {

        ISRuleInfoExample example = new ISRuleInfoExample();
        ISRuleInfoExample.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotBlank(req.getRuleType())) {
            criteria.andRuleTypeEqualTo(req.getRuleType());
        }

        if (StringUtil.isNotBlank(req.getRuleSwitch())) {
            criteria.andRuleSwitchEqualTo(req.getRuleSwitch());
        }

        long countByExample = iSRuleInfoMapper.countByExample(example);

        example.setOffset(req.start4Mysql());
        example.setLimit(req.getRows());
        List<ISRuleInfo> isRuleInfos = iSRuleInfoMapper.selectByExample(example);

        ISRulePageResult result = ResultBuilder.succPageResult(ISRulePageResult.class,
                req.getPage(), req.getRows(), Integer.parseInt(countByExample + ""));
        result.setIsBaseRuleDTOs(ISRuleConvert.convert2List(isRuleInfos));

        return result;
    }

    /**
     * 编辑规则
     *
     * @param req
     * @return
     */
    @Override
    public BaseResult editRule(ISBaseRuleReq req) {
        ISRuleInfo record = new ISRuleInfo();
        ISBaseRuleDTO isBaseRuleDTO = req.getIsBaseRuleDTO();
        record.setId(isBaseRuleDTO.getRuleId());
        record.setRuleSwitch(isBaseRuleDTO.getRuleSwitch());
        record.setGmtModifed(new Date());
        record.setOperator(isBaseRuleDTO.getOperator());
        Map<String, RuleData> ruleData = isBaseRuleDTO.getRuleData();
        String ruleTemplate = JSON.toJSONString(ruleData);
        record.setRuleTemplate(ruleTemplate);
        int result = iSRuleInfoMapper.updateByPrimaryKeySelective(record);
        if (result > 0) {
            return ResultBuilder.succ();
        }
        return ResultBuilder.fail(500, "修改失败");
    }

}
